package lsem.eclipse.vdf.ui.handler;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.widgets.Event;
import org.neo4j.kernel.Config;
import org.neo4j.neoclipse.Activator;
import org.neo4j.neoclipse.connection.Alias;
import org.neo4j.neoclipse.connection.ConnectionsView;
import org.neo4j.neoclipse.view.ErrorMessage;

public class SaveAliasAndStartDBHandler extends AbstractHandler implements
		IHandler {

	public final static String ID = SaveAliasAndStartDBHandler.class.getCanonicalName();

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		Event eventt = (Event) event.getTrigger();
		String projectName = eventt.text;

		IProject activeProject = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(projectName);

		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		IPath rootLocation = root.getLocation();

		try {
			Alias alias = new Alias(activeProject.getName(),
					rootLocation.toOSString()
							+ activeProject.getFullPath().toOSString()
							+ File.separator + "DataBase", "", "");
			alias.addConfiguration(Config.ALLOW_STORE_UPGRADE,
					Boolean.toString(true));

			alias.addConfiguration(Config.NODE_AUTO_INDEXING, "true");
			alias.addConfiguration(Config.RELATIONSHIP_AUTO_INDEXING, "true");

			Activator.getDefault().getAliasManager().addAlias(alias);

			ConnectionsView connectionsView = Activator.getDefault()
					.getConnectionsView();
			if (true) {
				connectionsView.startOrStopConnection(alias);
			}
		} catch (Exception e) {
			ErrorMessage.showDialog("Connection problem", e);
		}

		return null;
	}

}
